<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
    <title>Document</title>
</head>
<body>
     <input type="file" name="" id="file">
     <button onclick="doUpload()">提交</button>
     <img src="" alt="">
</body>
<script>
    //上传图片
    //显示图片
    function doUpload(){
        let file=$('#file').get(0).files[0]//获取上传图片
        let formdata=new FormData()
        formdata.append('hehe',file)
        $.ajax({
            url:'http://localhost:3000/file/upload',
            type:'POST',
            cache:false,
            data:formdata,
            processData:false,
            contentType:false,
            success:function(data){
                console.log(data)
                if(data.err==0){
                    $('img').attr('src',`http://localhost:3000${data.img}`)
                }else{
                    alert('上传失败请重试！')
                }
            },error:function(){
                console.log('ajax上传出错')
            }
        })
    }
</script>
</html>